package com.aaa.qy127.controller;

import com.aaa.qy127.entity.Book;
import com.aaa.qy127.service.BookService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.Result;
import com.aaa.qy127.util.UploadUtil;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/book")
@RestController
public class BookController {

    @Resource(name = "bookServiceImpl")
    private BookService bookService;

    /**
     * 查询全部数据 和 查询部分数据的 转发
     * @param book
     * @return
     */
    @RequestMapping("/selectBook")
    public Result selectBook(Book book){
        Result result = bookService.selBook(book);
        return result;
    }

    /**
     * 修改 状态 的转发 位置
     * @param bookid
     * @param status
     * @return
     */
    @RequestMapping("/updateStat")
    public  CodeAndMsg updateStat(Integer bookid,Integer status){
        CodeAndMsg codeAndMsg = bookService.updStatus(bookid, status);
        return codeAndMsg;
    }

    /**
     * 修改 或 新增 的 业务的 转发
     * @param book
     * @return
     */
    @RequestMapping("/updateAndInsertBook")
    public CodeAndMsg updateAndInsertBook(Book book){
        String url = book.getUrl();
       // System.out.println("controller 类"+url);
        String[] split = url.split(",");
        //System.out.println("controller 类"+ Arrays.toString(split));
        CodeAndMsg codeAndMsg = bookService.updAndInsBook(book);
        return codeAndMsg;
    }

    /**
     * 书籍图片上传
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Map upload(MultipartFile file){
        String url = UploadUtil.upload(file);
        //System.out.println(url);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",url);
        return map;
    }

    /**
     * 通过 bookid 查询所有的 该 book的图片 并 发送的前端
     * @param bookid
     * @return
     */
    @RequestMapping("/getImgByBookId")
    public List<Map<String, Object>> getImgByBookId(String bookid){
        List<Map<String, Object>> imgByBookId = bookService.getImgByBookId(bookid);
        //System.out.println(imgByBookId);
        return imgByBookId;
    }

    /**
     * 上传重新上传处的图片
     * @param file
     * @return
     */
    @RequestMapping("/uploadReload")
    public Map uploadReload(MultipartFile file){
        String url = UploadUtil.upload(file);
        //System.out.println(url);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",url);
        return map;
    }

    /**
     * 重新上传 图片 也就是修改图片
     * @param urlNew
     * @param urlOld
     */
    @RequestMapping("/updateBookUrl")
    public CodeAndMsg updateBookUrl(String urlNew,String urlOld){
        //System.out.println(urlNew);
        //System.out.println(urlOld);
        CodeAndMsg codeAndMsg = bookService.updateBookUrl(urlNew, urlOld);
        return codeAndMsg;
    }

    /**
     * 删除 图片
     * @param url
     * @return
     */
    @RequestMapping("/deleteBookUrl")
    public CodeAndMsg deleteBookUrl(String url){
        CodeAndMsg codeAndMsg = bookService.deleteBookUrl(url);
        return codeAndMsg;
    }

    /**
     * 查询所有的书籍的名字 和 id 通过 typeid 查询 书籍的名字 和 id
     * @return
     */
    @RequestMapping("/selectBookNameAndBookid")
    public List<Map<String,Object>> selectBookNameAndBookid(String typeid){
        List<Map<String, Object>> maps = bookService.selectBookNameAndBookid(typeid);
        return maps;
    }

    /**
     * 通过 bookid 查询 这个书籍的 出版社
     * @param bookid
     * @return
     */
    @RequestMapping("/selectPublisherAndBookid")
    public List<Map<String,Object>> selectPublisherAndBookid(String bookid){
        List<Map<String, Object>> maps = bookService.selectPublisherAndBookid(bookid);
        return maps;
    }

    /**
     * 获取到 所有的 书籍的信息
     * @return
     */
    @RequestMapping("/showBook")
    public List<Map<String, Object>> showBook(HttpServletRequest request){
        List<Map<String, Object>> allBook = bookService.getAllBook(request);
        return allBook;
    }

    /**
     * 获取所有的 类型的 信息
     * @return
     */
    @RequestMapping("/showTyid")
    public List<Map<String,Object>> showTyid(HttpServletRequest request){
        List<Map<String, Object>> maps = bookService.showTyid(request);
        return maps;
    }

    /**
     * 通过 bookid 获取 该 书籍的 所有的图片
     * @param bookid
     * @return
     */
    @RequestMapping("/showBookByBookid")
    public CodeAndMsg showBookByBookid(String bookid,HttpServletRequest request){
        CodeAndMsg codeAndMsg = bookService.showBookByBookid(bookid, request);
        return codeAndMsg;
    }

    /**
     * 通过 typid 查询出 该 类型的 书籍 的 信息
     * @param typeid
     * @param request
     * @return
     */
    @RequestMapping("/showBookBytypeid")
    public List<Map<String, Object>> showBookBytypeid(String typeid,HttpServletRequest request){
        List<Map<String, Object>> maps = bookService.showBookBytypeid(typeid, request);
        return maps;
    }

    /**
     * 按照 bookname 模糊查询
     * @param bookname
     */
    @RequestMapping("/selectSomeBookBybookname")
    public List<Map<String, Object>> selectSomeBookBybookname(String bookname,HttpServletRequest request){
        List<Map<String, Object>> maps = bookService.selectSomeBookBybookname(bookname, request);
        return maps;
    }

}
